﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using HamcoWcs.Models.TibcoMessage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HamcoWcs.Service.Pcon.PconPlcDbMsgParsers
{
    /// <summary>
    /// 码头验证货物信息
    /// </summary>
    public class PconMsgParse_ReadAbnormal : PconMsgParseBase
    {
        public override EnumPconPlcRequest RequestType => EnumPconPlcRequest.AbnormalRead;
        private readonly ILoggerService _logger;
        public PconMsgParse_ReadAbnormal(ILoggerService logger,
             IMapper mapper, WcsDbContext wcsDbContext,
            IMesCommunicateService mesCommunicateService)
            : base(mesCommunicateService, wcsDbContext, mapper)
        {
            _logger = logger;
        }


        private static Dictionary<string, int> _codeOffset = new Dictionary<string, int>()
        {
            { "CJMCV100", 596 },
            { "CJMCV200", 606 },
            { "CJMCV300", 616 },
            { "CJMCV400", 626 },
            { "CJMCV500", 636 },
            { "CJMCV600", 646 },
            { "CJMCV700", 656 },
            { "CJMCV800", 666 },
            { "CJMCV900", 676 },
            { "CJMIFB00", 686 },
            { "CJMIFC00", 696 },
        };

        public override void Deal(PconDto pconDto, PconPlc pconPlc)
        {
            if (!_codeOffset.Keys.Contains(pconDto.MachineName))
            {
                _logger.AddSystemErrorLog($"PconMsgParse->ReadAbnormal:{pconDto.DeviceName}非读码设备上报了读码异常");
                return;
            }

            string lunitId = "";
            string runitId = "";

            var buffer = pconPlc.DbInfo.FirstOrDefault(x => x.DbNum == 103)?.Buffer;
            if (buffer is null)
            {
                _logger.AddSystemErrorLog($"PconMsgParse->ReadAbnormal:{pconDto.DeviceName}设备上报了读码异常,读取DB块失败");
                return;
            }

            var offset = _codeOffset[pconDto.MachineName];
            lunitId = Encoding.ASCII.GetString(buffer, offset, 10).Trim('\0').Trim();


            if (pconDto.MachineName == "CJMCV300")
            {
                var roffset = _codeOffset["CJMCV200"];
                runitId = Encoding.ASCII.GetString(buffer, roffset, 10).Trim('\0').Trim();

            }
            if (pconDto.MachineName == "CJMCV700")
            {
                var roffset = _codeOffset["CJMCV800"];
                runitId = Encoding.ASCII.GetString(buffer, roffset, 10).Trim('\0').Trim();
            }

            try
            {
                _mesCommunicateService.PalletAlarmReport(pconDto, lunitId, runitId);
                _logger.AddWcsRunLog(pconDto.UnitId, $"{pconDto.DeviceName},读码异常：传入条码[{pconDto.UnitId}],左侧条码[{lunitId}],右侧条码[{runitId}]");
            }
            catch (Exception ex)
            {
                _logger.AddSystemErrorLog($"PconMsgParse->ReadAbnormal:{ex.Message}");
            }
           
        }
    }
}
